<?php

namespace Home\Controller;

use Think\Controller;

class CommonController extends Controller {

    function _initialize() {
        header("Content-type: text/html; charset=utf-8");
        $s_userid = session('userid');
        if ($s_userid == '') {
            $c_userid = cookie('userid');
            if ($c_userid > 0) {
                $info = M("user")->field("name")->where("id= '" . $c_userid . "'")->find();
                if (!empty($info['name'])) {
                    session('username', $info['name']);
                    session('userid', $c_userid);
                }
            }
        }
        $action = strtolower(CONTROLLER_NAME);
        $mod = strtolower(ACTION_NAME);
        $actions = array("member", "goods", "say");
        if (in_array($action, $actions)) {
            session('returnUrl', __SELF__);
        }
        if (in_array($action, array("member", "order")) && session('userid') == '') {
            session('returnUrl', "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . "");
            $this->redirect("Login/index");
        }
        $carts = json_decode(cookie("cart"), true);
         $url = $action . "/" . $mod;
         $cart_num = 0;
        if ($url == 'area/detail' or $url == 'cart/lists') {
            if ($carts) {
                foreach ($carts as $k => $v) {
                    $info = M("goods")->field("name,price,sid,discount")->where("id = " . $v['id'] . "")->find();
                    $sids[] = $info['sid'];
                    $carts[$k]['name'] = $info['name'];
                    $carts[$k]['price'] = $info['price'];
                    $carts[$k]['price_total'] = ($info['price'] - $info['discount']) * $v['num'];
                    $carts[$k]['discount'] = $info['discount'];
                    $carts[$k]['sid'] = $info['sid'];
                    $cart_num += $v['num'];
                    $cartsIds[] = $v['id'];
                }
            }
            if ($sids) {
                $sids = array_unique($sids);
                $cartsArr = array();
                $total_price = 0;
                foreach ($sids as $k => $v) {
                    $shop = M("shop")->field("name")->where("id = " . $v . "")->find();
                    $cartsArr[$k]['id'] = $v;
                    $cartsArr[$k]['name'] = $shop['name'];
                    $shop_price = 0;
                    foreach ($carts as $k2 => $v2) {
                        if ($v == $v2['sid']) {
                            $cartsArr[$k]['sub'][] = $v2;
                            $shop_price += ($v2['price'] - $v2['discount']) * $v2['num'];
                        }
                    }
                    $cartsArr[$k]['shop_price'] = sprintf("%.1f", $shop_price);
                    $total_price += $cartsArr[$k]['shop_price'];
                }
            }
        }else{
            foreach ($carts as $k => $v) {
                $cart_num += $v['num'];
            }
        }
        $this->assign('total_price', sprintf("%.1f", $total_price));
        $this->assign('cartsArr', $cartsArr);
        $this->assign('cartsIds', $cartsIds);
        $this->assign("cart_num", $cart_num);
        $this->assign("carts", $carts);
        $this->assign("threeLogin", getTableFile("accounts"));
        $this->assign("config", getTableConfig());
        $this->assign("mod", $mod);
        $this->assign("action", $action);
        $this->assign("sessionAreaId", cookie("area"));
        $this->assign("userid", $s_userid);
        $this->assign("username", session('username'));
    }

}
